package com.example.basicdemo.controller;


import com.alibaba.fastjson.JSONObject;
import com.example.basicdemo.model.DemoBaseResult;
import com.isyscore.os.dev.api.dmc.model.builder.*;
import com.isyscore.os.dev.api.dmc.model.domain.DataTypeBOOL;
import com.isyscore.os.dev.api.dmc.model.domain.TslDetailDomain;
import com.isyscore.os.dev.api.dmc.model.domain.TslItemDomain;
import com.isyscore.os.dev.api.dmc.model.enums.*;
import com.isyscore.os.dev.api.dmc.model.result.*;
import com.isyscore.os.dev.api.dmc.service.DmcService;
import com.isyscore.os.dev.util.IsyscoreHashMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/api/${api-module}/demo")
public class DemoController {
    @Autowired
    private DmcService dmcService;

    /**
     * 创建设备类
     * 创建设备
     * 修改设备属性
     * 获取设备详情
     * 查询设备属性
     */
    @GetMapping("addProduct")
    public DemoBaseResult addProduct() {
        AddProductRequestBuilder builder = new AddProductRequestBuilder();
        builder.setHeaders(new IsyscoreHashMap());
        builder.setAuthType(AuthenticationEnum.DEVICE_CORRESPONDS_TO_SECRET);
        builder.setCommunicationType(ProtocolEnum.MQTT);
        builder.setConnectType(ConnectTypeEnum.SUB_DEVICE);
        builder.setGroupId("group0");
        builder.setNetworkingType(NetworkingTypeEnum.ETHERNET);
        builder.setProductKey("CM4GNAc08WC");
        builder.setProductName("设备类" + new Random().nextInt(Short.MAX_VALUE + 1));
        builder.setProtocol(0);
        AddProductResult result = dmcService.addProduct(builder);
        JSONObject resultJson = JSONObject.parseObject(JSONObject.toJSONString(result));
        resultJson.put("message", "您已成功创建设备类, 其名称为: " + builder.getProductName() + ", 您可以前往设备集成-设备管理-设备类查看该设备类");
        DemoBaseResult.Data data = new DemoBaseResult.Data()
                .setParam(JSONObject.parseObject(JSONObject.toJSONString(result.getResponse().getParamMap())))
                .setResult(resultJson);

        //同时创建设备模型
        AddDeviceModelRequestBuilder modelBuilder = new AddDeviceModelRequestBuilder();
        modelBuilder.setHeaders(new IsyscoreHashMap());
        modelBuilder.setModelName("设备模型" + new Random().nextInt(Short.MAX_VALUE + 1));
        modelBuilder.setProductKey("CM4GNAc08WC");
        TslDetailDomain tsl = new TslDetailDomain();
        List<TslItemDomain> attributes = new ArrayList<>();
        tsl.setAttributes(attributes);
        modelBuilder.setTsl(tsl);

        DataTypeBOOL dataTypeBOOL = new DataTypeBOOL();
        dataTypeBOOL.setTrueValue("开");
        dataTypeBOOL.setFalseValue("关");
        dataTypeBOOL.setType("BOOL");

        TslItemDomain tslItem1 = new TslItemDomain();
        tslItem1.setAccessMode(ReadWriteTypeEnum.R);
        tslItem1.setContent(dataTypeBOOL);
        tslItem1.setDataType("ATTRIBUTE");
        tslItem1.setIdentifier("Status");
        tslItem1.setName("灯状态");
        attributes.add(tslItem1);

        TslItemDomain tslItem2 = new TslItemDomain();
        tslItem2.setAccessMode(ReadWriteTypeEnum.RW);
        tslItem2.setContent(dataTypeBOOL);
        tslItem2.setDataType("ATTRIBUTE");
        tslItem2.setIdentifier("command");
        tslItem2.setName("灯控制");
        attributes.add(tslItem2);

        //创建设备模型
        dmcService.addDeviceModel(modelBuilder);
        return new DemoBaseResult().setData(data);
    }

    private static String devId = "";

    @GetMapping("creatDevice")
    public DemoBaseResult creatDevice() {
        AddDeviceRequestBuilder builder = new AddDeviceRequestBuilder();
        builder.setHeaders(new IsyscoreHashMap());
        builder.setDevId("device" + new Random().nextInt(Short.MAX_VALUE + 1));
        builder.setDevName("设备" + new Random().nextInt(Short.MAX_VALUE + 1));
        builder.setExtra("设备创建测试");
        builder.setGroupId("group0");
        builder.setProductKey("CM4GNAc08WC");
        builder.setSecret("uCuGGnPUbtT745oU");
        AddDeviceResult result = dmcService.addDevice(builder);
        JSONObject resultJson = JSONObject.parseObject(JSONObject.toJSONString(result));
        resultJson.put("message", "您已成功创建设备, 其名称为: " + builder.getDevName() + ", 您可以前往设备集成-设备管理-设备查看该设备");
        DemoBaseResult.Data data = new DemoBaseResult.Data()
                .setParam(JSONObject.parseObject(JSONObject.toJSONString(result.getResponse().getParamMap())))
                .setResult(resultJson);
        devId = builder.getDevId();
        return new DemoBaseResult().setData(data);
    }

    @GetMapping("invokeDeviceService")
    public DemoBaseResult invokeDeviceService() {
        InvokeServiceRequestBuilder builder = new InvokeServiceRequestBuilder();
        builder.setHeaders(new IsyscoreHashMap());
        //TODO 设置参数

        InvokeServiceResult result = dmcService.invokeService(builder);
        JSONObject resultJson = JSONObject.parseObject(JSONObject.toJSONString(result));
        DemoBaseResult.Data data = new DemoBaseResult.Data()
                .setParam(JSONObject.parseObject(JSONObject.toJSONString(result.getResponse().getParamMap())))
                .setResult(resultJson);
        return new DemoBaseResult().setData(data);
    }

    @GetMapping("setAttributeValue")
    public DemoBaseResult setAttributeValue() {
        SetAttributeValueRequestBuilder builder = new SetAttributeValueRequestBuilder();
        builder.setHeaders(new IsyscoreHashMap());
        //TODO 设置参数
        builder.setDevId(devId);
        builder.setIdentifier("command");
        builder.setParam(true);
        SetAttributeValueResult result = dmcService.setAttributeValue(builder);
        JSONObject resultJson = JSONObject.parseObject(JSONObject.toJSONString(result));
        DemoBaseResult.Data data = new DemoBaseResult.Data()
                .setParam(JSONObject.parseObject(JSONObject.toJSONString(result.getResponse().getParamMap())))
                .setResult(resultJson);
        return new DemoBaseResult().setData(data);
    }

    @GetMapping("getAttributeValue")
    public DemoBaseResult getAttributeValue() {
        GetAttributeValueRequestBuilder builder = new GetAttributeValueRequestBuilder();
        builder.setHeaders(new IsyscoreHashMap());
        builder.setDevId(devId);
        builder.setIdentifier("command");
        GetAttributeValueResult result = dmcService.getAttributeValue(builder);
        JSONObject resultJson = JSONObject.parseObject(JSONObject.toJSONString(result));
        DemoBaseResult.Data data = new DemoBaseResult.Data()
                .setParam(JSONObject.parseObject(builder.toJsonString()))
                .setResult(resultJson);
        return new DemoBaseResult().setData(data);
    }

    @GetMapping("getDeviceDetail")
    public DemoBaseResult getDeviceDetail() {
        GetDeviceRequestBuilder builder = new GetDeviceRequestBuilder();
        builder.setHeaders(new IsyscoreHashMap());
        builder.setDevId(devId);
        GetDeviceResult result = dmcService.getDevice(builder);
        JSONObject resultJson = JSONObject.parseObject(JSONObject.toJSONString(result));
        resultJson.put("message", "您已成功获取设备详情");
        DemoBaseResult.Data data = new DemoBaseResult.Data()
                .setParam(JSONObject.parseObject(JSONObject.toJSONString(result.getResponse().getParamMap())))
                .setResult(resultJson);
        return new DemoBaseResult().setData(data);
    }



}
